﻿using IRepositoryBll;
using IRepositoryDal;
using RepositoryEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepositoryBll
{
    public class ConsumableInfoBll : IConsumableInfoBll
    {
        private IConsumableInfoDal _consumableInfoDal;
        private ICategoryDal _CategoryDal;
        private IConsumableRecordDal _consumableRecordDal;
        public ConsumableInfoBll(IConsumableInfoDal consumableInfoDal, ICategoryDal CategoryDal, IConsumableRecordDal consumableRecordDal)
        {
            _consumableInfoDal = consumableInfoDal;
            _CategoryDal = CategoryDal;
            _consumableRecordDal = consumableRecordDal;
        }

        /// <summary>
        /// 添加耗材信息
        /// </summary>
        /// <param name="consumableInfo"></param>
        /// <returns></returns>
        public bool AddConsumableInfo(ConsumableInfo consumableInfo)
        {
            return _consumableInfoDal.Add(consumableInfo);
        }

        /// <summary>
        /// 获取耗材ID
        /// </summary>
        /// <param name="consumableInfoId"></param>
        /// <returns></returns>
        public ConsumableInfo GetConsumableInfo(string consumableInfoId)
        {
            return _consumableInfoDal.GetEntity(consumableInfoId);
        }

        /// <summary>
        /// 获取集合、分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public object GetConsumableInfoListByPage(int page, int limit, out int count, string description)
        {
            //获取耗材信息集合
            var consumableInfo = _consumableInfoDal.GetEntitiesDb().Where((u => u.IsDelete == false));
            if (!string.IsNullOrEmpty(description))
            {
                consumableInfo = consumableInfo.Where(u => u.Description.Contains(description));
            }
            //统计数据数量
            count = consumableInfo.Count();

            //获取类型数据集
            var categorys = _CategoryDal.GetEntitiesDb();
            var tempList = from co in consumableInfo
                           join ca in categorys
                           on co.CategoryId equals ca.ID into cocaTemp
                           from coca in cocaTemp.DefaultIfEmpty()
                           select new
                           {
                               co.ID,
                               co.Description,
                               co.CategoryId,
                               co.Name,
                               co.Specification,
                               co.Num,
                               co.Unit,
                               co.Money,
                               co.WarningNum,
                               coca.CategoryName,
                               co.CreateTime
                           };

            var res = tempList.OrderBy(r => r.CreateTime).Skip((page - 1) * limit).Take(limit).ToList().Select(u => new
            {

                u.ID,
                u.Description,
                u.CategoryId,
                u.Name,
                u.Specification,
                u.Num,
                u.Unit,
                u.Money,
                u.WarningNum,
                u.CategoryName,
                CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")


            });

            return res;

            /*var list = consumableInfo.ToList().Select(u =>
            {
                return new
                {
                    u.ID,
                    u.Description,
                    u.CategoryId,
                    u.Name,
                    u.Specification,
                    u.Num,
                    u.Unit,
                    u.Money,
                    u.WarningNum,
                    
                    CreateTime = u.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
                };
            });*/


        }


        /// <summary>
        /// 获取耗材管理的耗材名字
        /// </summary>
        /// <returns></returns>
        public object GetConsumableInfoSelectOption()
        {
            //linq
            var Name = _consumableInfoDal.GetEntitiesDb().Select(d => new
            {
                d.ID,
                d.Name
            }).ToList();
            return Name;
        }

        //判断耗材名字是否相同
        public bool IsHasConsumbleInfo(string name)
        {
            return _consumableInfoDal.GetEntitiesDb().Where(c => !c.IsDelete && c.Name == name).Count() > 0;
        }


        /// <summary>
        /// 软删除
        /// </summary>
        /// <param name="consumableInfoId"></param>
        /// <returns></returns>
        public bool SoftConsumableInfo(string consumableInfoId, out string msg)
        {
            ConsumableInfo consumableInfo = _consumableInfoDal.GetEntity(consumableInfoId);

            bool isHad = _consumableRecordDal.GetEntitiesDb().Where(c => c.ConsumbleId == consumableInfoId).Count() > 0;
            if (!isHad)
            {
                if (consumableInfo != null)
                {
                    consumableInfo.IsDelete = true;
                    consumableInfo.DeleteTime = DateTime.Now;
                    bool isSuccess = _consumableInfoDal.Update(consumableInfo);
                    if (isSuccess)
                    {
                        msg = "删除成功";
                        return true;
                    }
                    else
                    {
                        msg = "删除失败";
                        return false;
                    }
                }
                else
                {
                    msg = "未找到要删除的数据";
                    return false;
                }
            }
            else
            {
                msg = "该耗材已录入了耗材记录，无法删除";
                return false;
            }





        }

        /// <summary>
        /// 更新耗材
        /// </summary>
        /// <param name="consumableInfo"></param>
        /// <returns></returns>
        public bool UpdateConsumableInfo(ConsumableInfo consumableInfo)
        {
            return _consumableInfoDal.Update(consumableInfo);
        }
        /// <summary>
        /// 更新耗材信息
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Description"></param>
        /// <param name="CategoryId"></param>
        /// <param name="Name"></param>
        /// <param name="Specification"></param>
        /// <param name="Num"></param>
        /// <param name="Unit"></param>
        /// <param name="Money"></param>
        /// <param name="WarningNum"></param>
        /// <returns></returns>
        public bool UpdateConsumableInfo(string consumableInfoId, string description, string categoryId, string name, string specification, int num, string unit, decimal money, int warningNum)
        {
            ConsumableInfo consumableInfo = _consumableInfoDal.GetEntity(consumableInfoId);
            if (consumableInfo != null)
            {
                consumableInfo.Description = description;
                consumableInfo.CategoryId = categoryId;
                consumableInfo.Name = name;
                consumableInfo.Specification = specification;
                consumableInfo.Num = num;
                consumableInfo.Unit = unit;
                consumableInfo.Money = money;
                consumableInfo.WarningNum = warningNum;

                return _consumableInfoDal.Update(consumableInfo);
            }
            else
            {
                return false;
            }
        }
    }
}
